//
//  WBNetworkManager+extension.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/15.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

extension WBNetworkManager{
    
    func statusList(since_id: Int64 = 0,max_id: Int64 = 0,completion:@escaping(_ list:[[String:Any]]?,_ isSucess:Bool)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"

        let params = [
            "since_id":since_id,
            "max_id":max_id > 0 ? (max_id - 1) : 0
        ]
        
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            let result = json as? [String:Any]
            
            let status = result?["statuses"] as? [[String:Any]]
            
            completion(status, isSuccess)
        }
    }
    
    ///加载未读的微博条数
    func unreadCount(completion:@escaping(_ statusCount: Int) -> ()) {
        
        guard let uid = userAccount.uid else {
            
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let param = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: param) { (json, isSuccess) in
            
            let dict = json as? [String:Any]
            
            let status = dict?["status"] as? Int
            
            completion(status ?? 0)
        }
    }
}


extension WBNetworkManager{
    
    func loadUserInfo(completion: @escaping (_ dict: [String: Any]) -> ()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        guard let uid = userAccount.uid else{
            
            return
        }
        
        let param = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: param) { (json, isSuccess) in
            
            completion((json as? [String: Any]) ?? [:])
        }
    }
}


// MARK: - 请求token的网络请求
extension WBNetworkManager{
    
    func loadAccess_token(code: String,completion:@escaping (_ isSuccess: Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id" : WBAppKey,
                     "client_secret" : WBAppSecret,
                     "grant_type" : "authorization_code",
                     "code" : code,
                     "redirect_uri" : WBRedirect_url,]
        
        request(WBHTTPMethod: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            
            //服务器传回token令牌是放在一个字典里面的
            guard let json = json else{
                
                return
            }
            
            self.userAccount.yy_modelSet(withJSON: json)
            
            self.loadUserInfo(completion: { (dict) in
                
                self.userAccount.yy_modelSet(withJSON: dict)
                
                self.userAccount.saveUserAccount()
                
                completion(isSuccess)
            })
        }
    }
}

// MARK: - 发布微博的接口
extension WBNetworkManager{
    
    func postStatus(text: String,completion:@escaping (_ result: Any?, _ isSuccess: Bool) -> ()) -> () {
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let params = ["status": text]
        
        tokenRequest(WBHTTPMethod: .POST, URLString: urlString, parameters: params) { (result, isSuccess) in
            
            completion(result, isSuccess)
        }
    }
}
